package ru.nsu.fit.iconveyor.example.math.node;

import ru.nsu.fit.iconveyor.conveyor.node.InputNode;
import ru.nsu.fit.iconveyor.conveyor.node.NodeConnection;

import java.util.*;

public class StdInNumberInputNode extends InputNode {
    public static final String OUTPUT_RESULT = "result";

    public static final String INPUT_NUMBER = "INPUT_NUMBER";

    private final NodeConnection<Double> outputResult;

    public StdInNumberInputNode(String id) {
        super(id);

        outputResult = new NodeConnection<Double>(Double.class, NodeConnection.Type.OUTPUT, OUTPUT_RESULT);
        addNodeConnection(outputResult);
    }

    @Override
    public Map<String, Object> execute(Map<String, Object> resource, Properties prop) {
        double number = 0;

        Object objNumber = getParameter(INPUT_NUMBER);
        if (objNumber != null) {
            try {
                number = Double.valueOf(objNumber.toString());
            } catch (Exception ex) {}
        }

        // Generate result
        Map<String, Object> bundles = new HashMap<String, Object>();
        bundles.put(outputResult.getName(), number);
        return bundles;
    }

    @Override
    public Map<String, Object> preExecute(Map<String, Object> availableInputResource, Properties prop) {
        return availableInputResource;
    }
}
